package com.leyou.item.controller;

import com.leyou.common.vo.PageResult;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author Eric
 * @version v1.0
 * @package com.leyou.item.controller
 * @date 2019/6/27 17:40
 * @description:
 **/
@RestController
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    /**
     * 分页查询商品列表
     * @param key
     * @param saleable
     * @param rows
     * @param page
     * @return
     */
    @GetMapping("spu/page")
    public ResponseEntity<PageResult<SpuDTO>>  queryGoods(
            @RequestParam(value = "key",required = false) String key,
            @RequestParam(value = "saleable",required = false) Boolean saleable,
            @RequestParam(value = "rows",defaultValue = "5") Integer rows,
            @RequestParam(value = "page",defaultValue = "1") Integer page
    ) {
        return ResponseEntity.ok(goodsService.queryGoods(key,saleable,rows,page));
    }


    /**
     * 新增商品
     * @param spuDTO
     * @return
     */
    @PostMapping("goods")
    public ResponseEntity<Void> insertGoods(@RequestBody SpuDTO spuDTO){

        goodsService.insertGoods(spuDTO);
        return ResponseEntity.ok().build();
    }

    /**
     * 商品下架
     * @param spuId
     * @param saleable
     * @return
     */
    @PutMapping("spu/saleable")
    public ResponseEntity<Void> updateSpuSaleable(@RequestParam("id")Long spuId,@RequestParam("saleable") Boolean saleable){
        goodsService.updateSpuSaleable(spuId,saleable);

        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


    /**
     * spuDetail数据回显
     * @param spuId
     * @return
     */
    @GetMapping("spu/detail")
    public ResponseEntity<SpuDetailDTO> querySpuDetail(@RequestParam("id") Long spuId){
        return ResponseEntity.status(HttpStatus.OK).body(goodsService.querySpuDetail(spuId));
    }


    /**
     * sku数据回显
     * @param spuId
     * @return
     */
    @GetMapping("sku/of/spu")
    public ResponseEntity<List<SkuDTO>> querySkuBySpuId(@RequestParam("id") Long spuId){
        return ResponseEntity.status(HttpStatus.OK).body(goodsService.queryListSku(spuId));
    }

    /**
     * 修改商品提交
     * @param spuDTO
     * @return
     */
    @PutMapping("goods")
    public ResponseEntity<Void> updateGoods(@RequestBody SpuDTO spuDTO){

        goodsService.updateGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


    /**
     * 根据spuID查询spu
     * @param spuId
     * @return sku的集合
     */
    @GetMapping("spu/list")
    public ResponseEntity<SpuDTO> querySpuBySpuId(@RequestParam("id") Long spuId){
        return ResponseEntity.ok(goodsService.querySpuBySpuId(spuId));
    }

    /**
     * 根据skuId查询sku信息
     * @param ids
     * @return
     */
    @GetMapping("sku/list")
    public ResponseEntity<List<SkuDTO>> querySkuByIds(@RequestParam("ids") List<Long> ids){
        return ResponseEntity.ok(goodsService.querySkuByIds(ids));
    }

    /**
     * 减库存
     * @param cartMap 商品id及数量的map
     */
    @PutMapping("/stock/minus")
    public ResponseEntity<Void> minusStock(@RequestBody Map<Long, Integer> cartMap){
        goodsService.minusStock(cartMap);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 加库存
     * @param map
     */
    @PutMapping("/stock/plus")
    public ResponseEntity<Void> plusStock(@RequestBody Map<Long,Integer> map){
        goodsService.plusStock(map);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
}
